package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.sky.dto.ViolationRecordPageQueryDTO;
import com.sky.entity.ViolationRecord;
import com.sky.mapper.ViolationRecordMapper;
import com.sky.result.PageResult;
import com.sky.service.ViolationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ViolationRecordServiceImpl implements ViolationRecordService {
    @Autowired
    private ViolationRecordMapper violationRecordMapper;
    @Override
    public PageResult mypageQuery(ViolationRecordPageQueryDTO pageQuery) {
        //1、设置当前页和每页条数
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        Page<ViolationRecord> page =violationRecordMapper.mypageQuery(pageQuery);
        long total = page.getTotal();
        List<ViolationRecord> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public ViolationRecord getById(Long id) {
        ViolationRecord violationRecord = violationRecordMapper.getById(id);
        return violationRecord;
    }

    @Override
    public PageResult pageQuery(ViolationRecordPageQueryDTO pageQuery) {
        //1、设置当前页和每页条数
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        Page<ViolationRecord> page =violationRecordMapper.pageQuery(pageQuery);
        long total = page.getTotal();
        List<ViolationRecord> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 批量删除
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        violationRecordMapper.deleteBatch(ids);
    }

@Override
    public void save(ViolationRecord violationRecord) {
        //1填充完整ViolationRecord

        //从数据库中查询违法等级
        violationRecord.setWarningLevel(violationRecordMapper.findWarningLevel(getViolationTypeLabel(violationRecord.getViolationType())));
        //状态为未读
        violationRecord.setStatus(ViolationRecord.UN_READ);
        //地点暂定
        violationRecord.setLocation("克拉玛依市克拉玛依区文轩路克拉玛依大学城");
        //匹配违法用户
        violationRecord.setUserId(employeeMapper.getIdByCarId(violationRecord.getCarId()));
        //调用mapper写入数据库
        violationRecordMapper.insert(violationRecord);

        //2、同步添加执法记录
        Execution execution = new Execution();
        BeanUtils.copyProperties(violationRecord,execution);
        execution.setId(null);
        execution.setViolationId(violationRecord.getId());
        execution.setCreateTime(null);
        execution.setUpdateTime(null);
        executionMapper.insert(execution);

        //3、判断预警等级并推送
        if (violationRecord.getWarningLevel() != null) {
            String message = buildWarningMessage(violationRecord);
            webSocketServer.sendToClient("admin",message);
        }

    }
}
